<?php
class AgencyModel extends model
{
	static $_instance = null;

	static function getInstance ()
	{
		if (is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	// 增加机构
	function AgencyAdd($p)
	{
		return $this->sql("insert into crm_agency (`name`,`tel`,`addres`,`contact`,`business`,`status`,`desc`,`parent`)values('[name]','[tel]','[addres]','[contact]','[business]','[status]','[desc]','[id]')",$p)->i();
	}
	
	// 修改机构
	function AgencyUpd($p)
	{
		return $this->sql("update crm_agency set `name`='[name]',`tel`='[tel]',`business`='[business]',`addres`='[addres]',`contact`='[contact]',`status`='[status]',`desc`='[desc]' where `id`='[id]'",$p)->u();
	}
	
	// 删除机构
	function AgencyDel($p)
	{
		return $this->sql("delete from crm_agency where `id`='[id]'",$p)->u();
	}
	
	// 机构使用数量
	function AgencyCount($p){
		$return = $this->file("AgencyCount",$p)->s(1);
		return $return["c"];
	}
	
	// 联系人信息
	function AgencyInfo($id)
	{
		return $this->sql("select * from crm_agency where id=$id")->s(1);
	}
	
	// 负责的机构
	function ChargeAgency($id)
	{
		return $this->sql("select id from crm_agency where find_in_set($id,contact)")->s();
	}
	
	// 负责的机构
	function getContact($id)
	{
		$rs = $this->sql("select group_concat(contact) as contact from crm_agency where find_in_set($id,business)")->s(1);
		if($rs){
			return $rs['contact'];
		}
		return "0";
	}
}